﻿using DevExpress.XtraEditors;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucMachineListAdd : XtraForm
    {
        private dic_machine _obj;
        private readonly ICommon<dic_machine> _exeDic;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucMachineListAdd(ICommon<dic_machine> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new dic_machine();
            SetDataIntoUI();
        }

        public xucMachineListAdd(dic_machine currObj, ICommon<dic_machine> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoUI();
        }

        private void xucMachineListAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction){
                case Actions.Update:
                    txtMaThietBi.Text = _obj.machinecode;
                    txtCongSo.Text = _obj.portid;
                    txtIP.Text = _obj.ip;
                    if (_obj.password!="") txtMauKhau.Text = StaticVariable.DecryptPassword(_obj.password);
                    txtTenThietBi.Text = _obj.machinename;
                    ddbCom.Text = _obj.com;
                    ddbLoaiCong.Text = _obj.porttype;
                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    _obj = new dic_machine();
                    txtMaThietBi.Text = _exeDic.NewID("MA");
                    txtTenThietBi.Text = "";
                    txtCongSo.Text = "";
                    txtIP.Text = "";
                    txtMauKhau.Text = "";
                    ddbCom.Text = @"COM 1";
                     ddbLoaiCong.Text = @"TCP/TP";

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new dic_machine();
                    txtMaThietBi.Text = _exeDic.NewID("MA");
                    txtTenThietBi.Text = "";
                    txtCongSo.Text = "";
                    txtIP.Text = "";
                    txtMauKhau.Text = "";
                       ddbCom.Text = @"COM 1";
                     ddbLoaiCong.Text = @"TCP/TP";

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtMaThietBi.Text == "" || txtTenThietBi.Text == "" || txtCongSo.Text == "" || txtIP.Text == "" || txtMauKhau.Text == "")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin thiết bị");
               
                return false;
            }
            _obj.machinecode = txtMaThietBi.Text;
            _obj.machinename = txtTenThietBi.Text;
            _obj.password = StaticVariable.ToEncryptPassword(txtMauKhau.Text);
            _obj.ip = txtIP.Text;
            _obj.com = ddbCom.Text;
            _obj.porttype = ddbLoaiCong.Text;
            _obj.portid = txtCongSo.Text;

            return true;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }
    }
}
